// pages/activationInfo/index.js
var http = require('../../utils/http');
var api = require('../../utils/api');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        checked: false,
        truename: "", //家长姓名
        name: "",
        idCard: "",
        fileListA: [],
        fileListB: [],
        fileListC: [],
        fileListD: [],
        fileListE: [],
        showDialog: false,
        errorYouFlag: false,
        errorFlag: false,
        errorIdCardFlag: false,
        errorImgA: false,
        errorImgB: false,
        errorImgC: false,
        phone: "",
        from: "", //如果==my  控制返回到上一页
    },
    jump1() {
        wx.showLoading({
            title: '打开中...',
        })
        wx.downloadFile({
            url: 'https://www.seemoreopt.com/api/readFiles/UserServe.pdf', //要预览的PDF的地址
            success: function (res) {
                console.log(res);
                if (res.statusCode === 200) { //成功
                    var Path = res.tempFilePath //返回的文件临时地址，用于后面打开本地预览所用
                    wx.openDocument({
                        filePath: Path, //要打开的文件路径
                        success: function (res) {
                            console.log('打开PDF成功');
                            wx.hideLoading()
                        }
                    })
                }
            },
            fail: function (res) {
                console.log(res); //失败
            }
        })
    },
    jump2() {
        wx.showLoading({
            title: '打开中...',
        })
        wx.downloadFile({
            url: 'https://www.seemoreopt.com/api/readFiles/ChildPrivacy.pdf', //要预览的PDF的地址
            success: function (res) {
                console.log(res);
                if (res.statusCode === 200) { //成功
                    var Path = res.tempFilePath //返回的文件临时地址，用于后面打开本地预览所用
                    wx.openDocument({
                        filePath: Path, //要打开的文件路径
                        success: function (res) {
                            console.log('打开PDF成功');
                            wx.hideLoading()
                        }
                    })
                }
            },
            fail: function (res) {
                console.log(res); //失败
            }
        })
    },
    jump3() {
        wx.showLoading({
            title: '打开中...',
        })
        wx.downloadFile({
            url: 'https://www.seemoreopt.com/api/readFiles/UserPrivacy.pdf', //要预览的PDF的地址
            success: function (res) {
                console.log(res);
                if (res.statusCode === 200) { //成功
                    var Path = res.tempFilePath //返回的文件临时地址，用于后面打开本地预览所用
                    wx.openDocument({
                        filePath: Path, //要打开的文件路径
                        success: function (res) {
                            console.log('打开PDF成功');
                            wx.hideLoading()
                        }
                    })
                }
            },
            fail: function (res) {
                console.log(res); //失败
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            phone: options.phone,
            name: options.childname,
            idCard: options.idcard,
            from: options.from,
            truename: options.truename == 'null' ? '' : options.truename
        })
    },
    onCloseDialog() {
        this.setData({
            showDialog: false
        });
    },
    onBackDialog() {
        this.setData({
            showDialog: false
        });
        if (this.data.from == 'my') {
            wx.navigateBack({
                delta: 1,
            })
            return
        }
        // if (this.data.phone) {
        //     wx.navigateTo({
        //         url: '/pages/register/index?phone=' + this.data.phone
        //     })
        //     return
        // }
        wx.clearStorageSync()
        wx.redirectTo({
            url: '/pages/loginHome/index'
        })
    },
    afterRead(event) {
        let _this = this
        const {
            file
        } = event.detail;
        const type = event.currentTarget.dataset.type
        wx.uploadFile({
            url: http.report_host + api.uploadFile, // 仅为示例，非真实的接口地址
            filePath: file.url,
            name: 'file',
            formData: {
                category: "personal_id"
            },
            success(res) {
                // 上传完成需要更新 fileList
                let doc = JSON.parse(res.data)
                // 上传完成需要更新 fileList
                const {
                    fileList = []
                } = _this.data;
                fileList.push({
                    ...file,
                    url: http.report_host + '/getFile/' + doc.data.file_id,
                    file_id: doc.data.file_id
                });
                switch (type) {
                    case "A":
                        _this.setData({
                            fileListA: fileList,
                            errorImgA: false
                        });
                        break;
                    case "B":
                        _this.setData({
                            fileListB: fileList,
                            errorImgA: false
                        });
                        break;
                    case "C":
                        _this.setData({
                            fileListC: fileList,
                            errorImgB: false
                        });
                        break;
                    case "D":
                        _this.setData({
                            fileListD: fileList,
                            errorImgC: false
                        });
                        break;
                    case "E":
                        _this.setData({
                            fileListE: fileList,
                            errorImgC: false
                        });
                        break;

                    default:
                        break;
                }

            },
        });
    },
    handleDelImg(e) {
        const type = e.currentTarget.dataset.type;
        console.log(type);
        switch (type) {
            case 'A':
                this.setData({
                    fileListA: []
                })
                break;
            case 'B':
                this.setData({
                    fileListB: []
                })
                break;
            case 'C':
                this.setData({
                    fileListC: []
                })
                break;
            case 'D':
                this.setData({
                    fileListD: []
                })
                break;
            case 'E':
                this.setData({
                    fileListE: []
                })
                break;

            default:
                break;
        }
    },
    onChangeCheckBox(event) {
        this.setData({
            checked: event.detail,
        });
    },
    /** 完成 */
    handleOver() {
        if (!this.data.checked) {
            wx.showModal({
                showCancel: false,
                content: '请阅读并确认政策与协议',
            })
            return
        }
        if (!this.data.name && !this.data.idCard && !this.data.truename) {
            this.setData({
                errorYouFlag: true,
                errorFlag: true,
                errorIdCardFlag: true
            })
            return
        }
        this.checkVIP('submit')
    },
    /** 身份证号input输入框失去焦点触发 */
    inputBlurFun() {
        this.checkVIP()
    },
    inputFocusFun() {
        this.setData({
            errorYouFlag: false,
            errorIdCardFlag: false,
            errorFlag: false
        })
    },
    /** 校验是否是会员 */
    checkVIP(type) {
        if (!this.data.truename) {
            this.setData({
                errorYouFlag: true
            })
            return
        }
        if (!this.data.idCard) {
            this.setData({
                errorIdCardFlag: true
            })
            return
        }
        if (!this.data.name) {
            this.setData({
                errorFlag: true
            })
            return
        }
        const sfzReg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
        if (!sfzReg.test(this.data.idCard)) {
            wx.showToast({
                icon: "error",
                title: '请输入正确的身份证',
            })
            return
        }
        http.postHttp(api.checkVIP, {
                personal_id: this.data.idCard,
                name: this.data.name
            },
            (result) => {
                if (result.code == 0) {
                    if (!result.data.if_vip) {
                        this.setData({
                            showDialog: true
                        })
                        return
                    }
                    if (type == 'submit') {
                        if (this.data.fileListA.length == 0 || this.data.fileListB.length == 0) {
                            this.setData({
                                errorImgA: true
                            })
                            return
                        }
                        if (this.data.fileListC.length == 0) {
                            this.setData({
                                errorImgB: true
                            })
                            return
                        }
                        if (this.data.fileListD.length == 0 || this.data.fileListE.length == 0) {
                            this.setData({
                                errorImgC: true
                            })
                            return
                        }
                        let p_id_files = [this.data.fileListA[0].file_id, this.data.fileListB[0].file_id,
                            this.data.fileListC[0].file_id, this.data.fileListD[0].file_id, this.data.fileListE[0].file_id,
                        ]

                        let obj = {
                            user: {
                                username: this.data.phone,
                                name: this.data.truename
                            },
                            child: [{
                                name: this.data.name,
                                personal_id: this.data.idCard,
                                p_id_files: p_id_files + ''
                            }]
                        }
                        const sfzReg = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
                        if (!sfzReg.test(this.data.idCard)) {
                            wx.showToast({
                                icon: "error",
                                title: '请输入正确的身份证',
                            })
                            return
                        }
                        http.postHttp(api.register, obj,
                            (result) => {
                                if (result.code == 0) {
                                    let childInfos = wx.getStorageSync('childInfo')
                                    if (childInfos) {
                                        childInfos.if_vip = true
                                        childInfos.p_id_files = '1,2,3'
                                        wx.setStorageSync('childInfo', childInfos)
                                    }
                                    wx.switchTab({
                                        url: '/pages/index/index',
                                    })
                                }
                            },
                            (err) => {
                                console.log(err);
                            },
                        )
                    }
                }

            },
            (err) => {
                console.log(err);
            },
        )

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})